import {
  getMayLikeNews,
  getInformations,
  getSearchWords,
  getSiteMaps
} from '@/api/information_home'
import { getProfessionHome } from '@/api/profession_home'
import tool from '@/utils/tool'
export default {
  namespaced: true,
  state: () => ({
    // 资讯数组
    information_list: [],
    professions: [],
    areas: [],
    navs: [],
    type: 1,
    old_nav_Id: '',
    informationPager: {
      page: 1,
      limit: 10,
      total: 0
    },
    // 关键字数组
    searchWord_list: [],
    // 网站地图
    siteMap_list: [],
    //栏目id
    nav_id: '',
    // 地区id
    area_id: '',
    //专业id
    profession_id: '',
    // 你可能感兴趣的资讯
    interestList: []
  }),
  mutations: {
    // 设置资讯数组
    setInformationList(state, data) {
      state.informationPager.total = +data.total
      state.information_list = tool.encodeString(data.data)
      // state.information_list = data.data
    },
    //设置关键词数组
    setSearchWordList(state, data) {
      state.searchWord_list = tool.encodeString(data)
    },
    // 设置网站地图数组
    setSiteMapList(state, data) {
      state.siteMap_list = tool.encodeString(data)
    },
    // 设置专业、地区、栏目
    setProfessionAreaNav(state, data) {
      state.professions = tool.encodeString(data.professionList)
      // state.professions =  data.professionList
      // 地区删除香港，澳门，台湾
      let areaList = data.areaList.filter(
        item => item.path !== 'hk' && item.path !== 'tw' && item.path !== 'mo'
      )
      state.areas = tool.encodeString(areaList)
      // state.areas = data.areaList.filter(item=>item.path !=='hk' && item.path !=='tw' && item.path !=='mo')
      state.navs = tool.encodeString(data.navList)
      // state.navs =  data.navList
    },
    // 设置类别id
    setNavId(state, data) {
      state.nav_id = data
    },
    //设置类别所属栏目id
    setOldNavId(state, data) {
      state.old_nav_Id = data
    },
    // 设置专业id
    setProfessionId(state, data) {
      state.profession_id = data
    },
    // 设置地区id
    setAreaId(state, data) {
      state.area_id = data
    },
    // 设置获取类型
    setType(state, type) {
      state.type = type
    },
    // 设置页数
    setPage(state, page) {
      state.informationPager.page = page
    },
    // 单独跟新资讯数组
    UPDATA_INFORMATION_LIST(state, data) {
      state.information_list = data
    },
    // 你可能感兴趣的资讯
    SET_INTERES_TLIST(state, data) {
      state.interestList = data
    }
  },
  actions: {
    // 获取你可能感兴趣的资讯
    async getInterestList({ commit, state }, params) {
      let list = []
      try {
        let res = await getMayLikeNews(params)
        if (res.data && res.data.data) {
          list = res.data.data
        }
      } catch (err) {
        list = []
      }

      if (Array.isArray(list)) {
        commit('SET_INTERES_TLIST', list)
      }
      return true
    },
    // 获取资讯数据
    async getInformationList({ commit, state }, params) {
      let {
        profession_id,
        nav_id,
        nav_parent_id,
        area_id,
        isServiceCache
      } = params
      let pa = { profession_id, nav_id }
      if (+nav_parent_id !== 4) {
        pa.area_id = area_id
      }
      commit('setNavId', nav_id)
      commit('setProfessionId', profession_id)
      commit('setAreaId', area_id)
      pa.page = state.informationPager.page
      pa.type = state.type
      if (tool.getType(isServiceCache) === 'boolean') {
        pa.isServiceCache = isServiceCache
      }
      let res = await getInformations(pa).catch(err => {
        return { data: { data: {}, total: 0 } }
      })

      res && res.data && commit('setInformationList', res.data)
      return true
    },
    // 获取关键词数据
    async getsearchWordList({ commit, state }, params) {
      let res = await getSearchWords(params).catch(err => {
        return { data: [] }
      })

      res &&
        res.data &&
        Array.isArray(res.data) &&
        commit('setSearchWordList', res.data)
      return true
    },
    // 获取网站地图数据
    async getSiteMapList({ commit, state }, params) {
      let res = await getSiteMaps(params).catch(err => {
        return { data: [] }
      })
      res &&
        res.data &&
        Array.isArray(res.data) &&
        commit('setSiteMapList', res.data)
      return true
    },
    // 获取专业首页数据
    async getProfessionHome({ commit, state, rootState }, params) {
      commit('setProfessionId', params.profession_id)
      let res = await getProfessionHome(params).catch(err => {
        return { data: {} }
      })
      // 设置顶部导航栏
      let arr = (res.data && res.data.menu) || []
      // rootState.navList = (res.data && res.data.menu) || []
      rootState.navList = tool.encodeString(arr)
    }
  },
  getters: {}
}
